/*
 * @Copyright: 2016 www.yyfax.com Inc. All rights reserved.
 */
package com.jaws.core.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 校验工具类
 * 
 * @author jiangmy
 * @date 2016-12-07 16:00:18
 */
public class ValidateUtil {
	private static Logger logger = LoggerFactory.getLogger(ValidateUtil.class);
	
	/** 纯汉字字符串判断 */
	public static final String CN_CHARS_REG = "^[\u4e00-\u9fa5·]+$";

	private ValidateUtil() {
	}

	/**
	 * 是否matches正则
	 * 
	 * @author jiangmy
	 * @date 2016-12-13 14:01:02
	 * @param str
	 * @param regex
	 * @return
	 */
	public static boolean check(String str, String regex) {
		if (StringUtils.isBlank(str)) {
			return false;
		}
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}

	static String IS_EMAIL_REGEX = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";

	/**
	 * 验证邮箱
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		return check(email, IS_EMAIL_REGEX);
	}

	static String IS_MOBILE_REGEX = "^0?1[1-9]\\d{9}$";

	/**
	 * 验证手机号码(13,14,15,17,18开头的11位数是手机号)
	 * 
	 * @param cellphone
	 * @return
	 */
	public static boolean isMobile(String cellphone) {
		return check(cellphone, IS_MOBILE_REGEX);
	}

	// static String IS_PHONE_REGEX = "^(010|02\\d|0[3-9]\\d{2})?\\d{6,8}$";
	//
	// /**
	// * 验证固话号码(不含3位紧急呼叫号码,5位服务号码)
	// *
	// * @param telephone
	// * @return
	// */
	// public static boolean isPhone(String telephone) {
	// return check(telephone, IS_PHONE_REGEX);
	// }

	static String IS_FAX_REGEX = "^(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)$";

	/**
	 * 验证传真号码
	 * 
	 * @param fax
	 * @return
	 */
	public static boolean isFax(String fax) {
		return check(fax, IS_FAX_REGEX);
	}

	/**
	 * 验证真实姓名<br>
	 * 
	 * @author buyi
	 * @date 2017-04-26 15:24:58
	 * @since v1.0.0
	 * @param name
	 * @return 如果校验通过，则返回true；反之，则返回false
	 */
	public static boolean isName(String name) {
		if (StringUtils.isBlank(name)) {
			return false;
		}

		// String reg = "^[\u4e00-\u9fa5]+$";
		Pattern pattern = Pattern.compile(CN_CHARS_REG);
		return pattern.matcher(name.trim()).find();
	}
	
//	public static void main(String[] args) {
//		String cellphone = "18033322222";
//		String cellphone2 = "19933322222";
//		String cellphone3 = "10933322222";
//		System.out.println(cellphone+":"+isMobile(cellphone));;
//		System.out.println(cellphone2+":"+isMobile(cellphone2));;
//		System.out.println(cellphone3+":"+isMobile(cellphone3));;
//	}
}
